import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get.dart';
import 'package:high_help_apps/common/utils/functions.dart';
import 'package:high_help_apps/common/values/colors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';
import 'help_each_other_logic.dart';
import 'help_each_other_state.dart';

class HelpEachOtherPage extends StatefulWidget {
  @override
  _HelpEachOtherPageState createState() => _HelpEachOtherPageState();
}

class _HelpEachOtherPageState extends State<HelpEachOtherPage>
    with AutomaticKeepAliveClientMixin {
  final HelpEachOtherLogic logic = Get.put(HelpEachOtherLogic());
  final HelpEachOtherState state = Get.find<HelpEachOtherLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RefreshIndicator(
        color: AppColors.primaryElement,
        onRefresh: () => logic.loadMessageInfo(),
        child: Container(
          color: Color.fromRGBO(248, 248, 248, 1),
          child: GetBuilder<HelpEachOtherLogic>(
            builder: (logic) => logic.isInit == false
                ? Center(
                    child: CircularProgressIndicator(
                      color: AppColors.primaryElement,
                    ),
                  )
                : ListView.separated(
                    padding: EdgeInsets.symmetric(
                      horizontal: 10,
                      vertical: 5,
                    ),
                    itemCount:
                        logic.state.messageInfoEntity.detail.records.length,
                    separatorBuilder: (BuildContext context, int index) {
                      return SizedBox(height: 10.h);
                    },
                    itemBuilder: (BuildContext context, int index) {
                      return Slidable(
                        actionPane: SlidableBehindActionPane(),
                        actionExtentRatio: 0.2,
                        secondaryActions: [
                          IconSlideAction(
                            caption: "联系我",
                            color: AppColors.primaryElement,
                            icon: Icons.add_circle,
                            foregroundColor: Colors.white,
                            onTap: () async => sTelephone(logic.state.messageInfoEntity.detail
                                .records[index].releaseUser.phone),
                          ),
                        ],
                        child: Container(
                          padding: EdgeInsets.all(10.r),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(8.r),
                          ),
                          child: ListTile(
                            leading: ClipOval(
                              child: Image.network(
                                "http://" +
                                    logic.state.messageInfoEntity.detail
                                        .records[index].releaseUser.avatar,
                                fit: BoxFit.cover,
                                height: 45.r,
                                width: 45.r,
                              ),
                            ),
                            title: Text(logic.state.messageInfoEntity.detail
                                .records[index].releaseUser.nickName),
                            subtitle: Text(logic.state.messageInfoEntity.detail
                                .records[index].desc),
                          ),
                        ),
                      );
                    },
                  ),
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<HelpEachOtherLogic>();
    super.dispose();
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
